<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 搜索 -->
        <el-form>
          <el-form-item prop="title">
            目录名称:
            <el-input style="width: 200px;" class="filter-item" v-model="query.directoryName"></el-input>
            状态:
            <el-select v-model="value" placeholder="请选择">
              <el-option value="1" label="启用"></el-option>
              <el-option value="0" label="禁用"></el-option>
            </el-select>
            <el-button class="filter-item" size="small" type="primary" @click="handleClear">清除</el-button>
            <el-button class="filter-item" size="small" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            <el-button class="filter-item fr" size="small" style="margin-left: 10px;" type="success" icon="el-icon-edit" @click="isShow = true">新增目录</el-button>
          </el-form-item>
        </el-form>
        <el-alert :title='titleText' type="info" show-icon>
        </el-alert>
        <!-- 数据 -->
        <el-table fit highlight-current-row :data="List" style="width: 100%">
          <el-table-column align="left" prop="id" label="序号" width="60px">
          </el-table-column>
          <el-table-column align="left" prop="subjectName" label="所属学科">
          </el-table-column>
          <el-table-column align="left" prop="directoryName" label="目录名称">
          </el-table-column>
          <el-table-column align="left" prop="username" label="创建者">
          </el-table-column>
          <el-table-column align="left" prop="addDate" label="创建日期">
            <template v-slot="{row}">
              <span>{{row.addDate | parseTimeByString("{y}-{m}-{d} {h}:{i}:{s}")}}</span>
            </template>
          </el-table-column>
          <el-table-column align="left" prop="totals" label="面试题数量">
          </el-table-column>
          <el-table-column align="left" prop="state" label="状态">
            <template v-slot="{row}">
              <span>{{row.state===1?'已启用':'已禁用'}}</span>
            </template>
          </el-table-column>
          <!-- 操作 -->
          <el-table-column label="操作" width="140px" prop="state">
            <template v-slot="{row}">
              <el-link type="primary" :underline="false" @click="handleChange(row)">
                {{row.state===1?'禁用':'启用'}}
              </el-link>
              <template v-if="row.state">
                <el-link type="primary" :underline="false" @click="handleMod(row)">修改</el-link>
                <el-link type="primary" :underline="false" @click="handleDel(row)">删除</el-link>
              </template>
              <template v-else>
                <el-link type="info" disabled :underline="false">修改</el-link>
                <el-link type="info" disabled :underline="false">删除</el-link>
              </template>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="block">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="query.page" :page-sizes="[2,5, 10, 20, 50]" :page-size="10" layout="->, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </el-card>
      <!-- 弹窗 -->
      <el-dialog :title="title" :visible.sync="isShow" width="400px" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
          <!-- 所属学科 -->
          <el-form-item label="所属学科" prop="subjectID">
            <el-select v-model="ruleForm.subjectID" placeholder="请选择" size="medium">
              <el-option v-for="item in List" :key="item.id" :label="item.subjectName" :value="item.subjectID"></el-option>
            </el-select>
          </el-form-item>
          <!-- 目录名称 -->
          <el-form-item label="目录名称" prop="directoryName">
            <el-input v-model="ruleForm.directoryName" size="medium"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { list, remove, add, update, detail, changeState } from '@/api/hmmm/directorys.js'
export default {
  data () {
    return {
      // 表单规则
      rules: {
        subjectName: [{ required: true, message: '请选择所属学科' }
        ],
        directoryName: [{ required: true, message: '请输入目录名称', trigger: 'blur' }]
      },
      // 弹窗表单 属性
      ruleForm: {
        subjectID: null,
        directoryName: null,
        id: null
      },
      // 弹窗显示 隐藏
      isShow: false,
      value: null,
      // 提示消息
      titleText: '',
      // 列表总数
      total: null,
      // 渲染列表数据
      List: [],
      // 查询参数
      query: {
        page: 1,
        pagesize: 10,
        state: null,
        directoryName: null
      }
    }
  },
  computed: {
    title () {
      return this.ruleForm.id ? '修改目录' : '添加目录'
    }
  },
  methods: {
    // 改变目录状态
    async handleChange (obj) {
      if (obj.state) {
        obj.state = 0
      } else {
        obj.state = 1
      }
      const ret = await changeState(obj)
      if (ret.status === 200) {
        // 修改成功
        this.$message.success('操作成功')
      }
    },
    // 修改目录 获取数据
    async handleMod (obj) {
      this.ruleForm.id = obj.id
      this.isShow = true
      const ret = await detail(obj)
      this.ruleForm.subjectID = ret.data.subjectID
      this.ruleForm.directoryName = ret.data.directoryName
    },
    // 添加 修改目录
    handleSubmit () {
      if (this.ruleForm.id) {
        // 修改目录
        this.$refs.ruleForm.validate(async (valid) => {
          if (!valid) return
          const ret = await update(this.ruleForm)
          if (ret.status === 200) {
            // 修改成功 重现渲染 清空表单 关闭表单
            this.getList()
            this.$refs.ruleForm.resetFields()
            this.isShow = false
            this.$message.success('修改成功')
          }
        })
      } else {
        // 添加目录
        this.$refs.ruleForm.validate(async (valid) => {
          if (!valid) return
          const ret = await add(this.ruleForm)
          if (ret.status === 200) {
            // 添加成功 重现渲染 清空表单 关闭表单
            this.$message.success('添加成功')
            this.getList()
            this.$refs.ruleForm.resetFields()
            this.isShow = false
          }
        })
      }
    },
    // 关闭弹窗
    handleClose () {
      this.isShow = false
      // 清空表单
      this.$refs.ruleForm.resetFields()
      this.ruleForm.id = null
    },
    // 删除
    handleDel (obj) {
      this.$confirm('确认要删除吗？, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const ret = await remove(obj)
          if (ret.status === 200) {
            // 删除成功
            this.$message.success('删除成功')
            // 重新渲染列表
            this.getList()
            const num = Math.ceil((this.total - 1) / this.query.pagesize)
            this.query.page = this.query.page > num ? num : this.query.page
          }
        })
        .catch(() => {
          this.$message.info('已取消操作!')
        })
    },
    // 清除
    handleClear () {
      this.value = null
      this.query.state = null
      this.query.directoryName = null
    },
    // 查询
    async handleSearch () {
      if (!this.value && !this.query.directoryName) {
        this.getList()
      } else {
        this.query.state = this.value
        this.query.page = 1
        const ret = await list(this.query)
        if (ret.status === 200) {
          // 查询数据成功
          this.List = ret.data.items
          this.total = ret.data.counts
          this.titleText = `数据一共${this.total}条`
        }
      }
    },
    // 调整每页的个数
    handleSizeChange (val) {
      this.query.pagesize = val
      this.getList(this.query)
    },
    // 跳转页码
    handleCurrentChange (val) {
      this.query.page = val
      this.getList()
    },
    // 获取列表数据
    async getList () {
      const ret = await list(this.query)
      console.log(ret)
      this.List = ret.data.items
      this.total = ret.data.counts
      this.titleText = `数据一共${this.total}条`
    }
  },
  // 初始化
  created () {
    this.getList()
  }

}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
  // text-align: right;
}
</style>

<style scoped>
/* .cell {
  display: flex;
  justify-content: space-between;
} */
.el-link--primary,
.el-link--info,
.el-link--inner {
  padding-right: 10px;
}
.el-table th {
  background-color: #fafafa;
}
.el-table th.is-leaf {
  border-bottom: 2px solid #e8e8e8;
}
</style>
